{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "! pip install -qU memorizz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import getpass\n",
    "import os\n",
    "\n",
    "# Function to securely get and set environment variables\n",
    "def set_env_securely(var_name, prompt):\n",
    "    value = getpass.getpass(prompt)\n",
    "    os.environ[var_name] = value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "set_env_securely(\"MONGODB_URI\", \"Enter your MongoDB URI: \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "set_env_securely(\"OPENAI_API_KEY\", \"Enter your OpenAI API Key: \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from memorizz.memory_provider.mongodb.provider import MongoDBConfig, MongoDBProvider\n",
    "\n",
    "# Create a memory provider\n",
    "mongodb_config = MongoDBConfig(uri=os.environ[\"MONGODB_URI\"])\n",
    "memory_provider = MongoDBProvider(mongodb_config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from memorizz.long_term_memory import KnowledgeBase\n",
    "\n",
    "# Create a knowledge base\n",
    "kb = KnowledgeBase(memory_provider)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Sample knowledge to ingest\n",
    "company_info = \"\"\"\n",
    "Acme Corporation is a fictional company that manufactures everything from portable holes to rocket-powered devices.\n",
    "Founded in 1952, Acme has been a leading supplier of innovative products for over 70 years.\n",
    "The company is headquartered in California and has 5,000 employees worldwide.\n",
    "Their most popular products include the Portable Hole, Rocket Skates, and Giant Rubber Band.\n",
    "\"\"\"\n",
    "\n",
    "product_info = \"\"\"\n",
    "Acme's Portable Hole is a revolutionary product that creates a temporary hole in any surface.\n",
    "Simply place the Portable Hole on a wall, floor, or ceiling, and it creates a passage to the other side.\n",
    "The hole can be folded up and carried in a pocket when not in use.\n",
    "Warning: Do not stack Portable Holes or place them face-to-face, as this may create a rift in space-time.\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ingesting knowledge...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Created knowledge entries with IDs: b3be3a5c-0e0f-49f8-b36a-152d97ce8482, 778b2b7c-6045-4b52-a4f8-8fbe2fe0d1f3\n"
     ]
    }
   ],
   "source": [
    "# Ingest the knowledge\n",
    "print(\"Ingesting knowledge...\")\n",
    "company_memory_id = kb.ingest_knowledge(company_info, namespace=\"company_info\")\n",
    "product_memory_id = kb.ingest_knowledge(product_info, namespace=\"product_info\")\n",
    "print(f\"Created knowledge entries with IDs: {company_memory_id}, {product_memory_id}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from memorizz import MemAgent\n",
    "\n",
    "# Create a simple agent\n",
    "agent = MemAgent(\n",
    "    instruction=\"You are a helpful assistant that provides information about Acme Corporation and its products.\",\n",
    "    memory_provider=memory_provider\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:src.memorizz.memagent:Memagent d3d12ee1-da11-4b35-8245-78c6f1af81dd saved in the memory provider\n",
      "INFO:src.memorizz.memagent:{\n",
      "    \"model\": null,\n",
      "    \"agent_id\": \"d3d12ee1-da11-4b35-8245-78c6f1af81dd\",\n",
      "    \"tools\": null,\n",
      "    \"persona\": null,\n",
      "    \"instruction\": \"You are a helpful assistant that provides information about Acme Corporation and its products.\",\n",
      "    \"memory_mode\": \"general\",\n",
      "    \"max_steps\": 20,\n",
      "    \"memory_ids\": [],\n",
      "    \"tool_access\": \"private\",\n",
      "    \"long_term_memory_ids\": null\n",
      "}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Created agent with ID: d3d12ee1-da11-4b35-8245-78c6f1af81dd\n"
     ]
    }
   ],
   "source": [
    "# Save the agent to get an agent_id\n",
    "agent.save()\n",
    "print(f\"Created agent with ID: {agent.agent_id}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:src.memorizz.memagent:Memagent d3d12ee1-da11-4b35-8245-78c6f1af81dd updated in the memory provider\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Attached company info to agent\n"
     ]
    }
   ],
   "source": [
    "# Method 1: Attach knowledge using the KnowledgeBase\n",
    "kb.attach_to_agent(agent, company_memory_id)\n",
    "print(\"Attached company info to agent\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "ERROR:src.memorizz.memagent:Error adding long-term memory to agent d3d12ee1-da11-4b35-8245-78c6f1af81dd: MemAgent with id d3d12ee1-da11-4b35-8245-78c6f1af81dd already exists\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Added product info via agent method\n"
     ]
    }
   ],
   "source": [
    "# Method 2: Attach knowledge using the agent's method\n",
    "agent.add_long_term_memory(product_info, namespace=\"additional_product_info\")\n",
    "print(\"Added product info via agent method\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Agent has 2 knowledge entries:\n",
      "1. Namespace: company_info\n",
      "   Content: \n",
      "Acme Corporation is a fictional company that manu...\n",
      "2. Namespace: additional_product_info\n",
      "   Content: \n",
      "Acme's Portable Hole is a revolutionary product t...\n"
     ]
    }
   ],
   "source": [
    "# Retrieve and print all long-term memories associated with the agent\n",
    "memories = agent.retrieve_long_term_memory()\n",
    "print(f\"\\nAgent has {len(memories)} knowledge entries:\")\n",
    "for i, memory in enumerate(memories, 1):\n",
    "    print(f\"{i}. Namespace: {memory.get('namespace')}\")\n",
    "    print(f\"   Content: {memory.get('content')[:50]}...\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Running queries with the agent:\n",
      "\n",
      "Query: What is Acme Corporation?\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/responses \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Response: Acme Corporation is a well-known fictional company featured in cartoons, comics, and pop culture, often recognized for producing a wide range of imaginative and sometimes outlandish products. Founded in 1952 and headquartered in California, Acme has a legacy of over 70 years and employs around 5,000 people worldwide.\n",
      "\n",
      "The company is most famous for its innovative and unusual products, many of which are frequently used by characters in classic animated shows. Popular Acme products include:\n",
      "\n",
      "- Portable Hole: A device that creates a temporary passage through any surface.\n",
      "- Rocket Skates: High-speed skates powered by rockets.\n",
      "- Giant Rubber Band: Often used for slingshot-like purposes in comedic scenarios.\n",
      "\n",
      "Acme Corporation is celebrated for its creative and unexpected solutions, often serving as the go-to brand for characters in need of unusual gadgets or contraptions. While it is a fictional entity, its products and brand have become a staple of popular culture.\n",
      "\n",
      "Query: Tell me about the Portable Hole.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/responses \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Response: Certainly! The Portable Hole is one of Acme Corporation’s most iconic and inventive products.\n",
      "\n",
      "What is the Portable Hole?\n",
      "The Portable Hole is a compact, flexible device designed to create an instant opening or passage through virtually any solid surface, including walls, floors, and ceilings. Simply place the Portable Hole on the desired surface, and it forms a functional hole, granting access to the space beyond.\n",
      "\n",
      "Key Features:\n",
      "\n",
      "- Portable & Flexible: The hole is lightweight and can be folded up for easy carrying in a pocket or bag.\n",
      "- Universal Application: Works on multiple surfaces—walls, floors, ceilings, and more.\n",
      "- Instant Deployment: Creates a passage immediately upon placement.\n",
      "\n",
      "Safety Information:\n",
      "- Important: Never stack multiple Portable Holes or place them face-to-face. Doing so can create a dangerous rift in space-time.\n",
      "- Recommended use is for temporary, non-permanent passages only.\n",
      "\n",
      "Fun Fact:\n",
      "The Portable Hole has appeared in countless cartoons and stories as a symbol of Acme’s imaginative engineering.\n",
      "\n",
      "If you’d like technical details, usage instructions, or a sense of humor in your answer, feel free to ask!\n",
      "\n",
      "Query: When was Acme founded?\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/responses \"HTTP/1.1 200 OK\"\n",
      "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Response: Acme Corporation was founded in 1952.\n"
     ]
    }
   ],
   "source": [
    "# Run some queries to demonstrate knowledge retrieval\n",
    "print(\"\\nRunning queries with the agent:\")\n",
    "\n",
    "queries = [\n",
    "    \"What is Acme Corporation?\",\n",
    "    \"Tell me about the Portable Hole.\",\n",
    "    \"When was Acme founded?\"\n",
    "]\n",
    "\n",
    "for query in queries:\n",
    "    print(f\"\\nQuery: {query}\")\n",
    "    response = agent.run(query)\n",
    "    print(f\"Response: {response}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "ERROR:src.memorizz.memagent:Error deleting long-term memory b3be3a5c-0e0f-49f8-b36a-152d97ce8482: MemAgent with id d3d12ee1-da11-4b35-8245-78c6f1af81dd already exists\n",
      "INFO:src.memorizz.memagent:MemAgent d3d12ee1-da11-4b35-8245-78c6f1af81dd deleted from the memory provider\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Cleaning up...\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Clean up (optional)\n",
    "print(\"\\nCleaning up...\")\n",
    "agent.delete_long_term_memory(company_memory_id)\n",
    "agent.delete()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "memorizz",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
